package com.hr.reactive.flux;

import com.hr.reactive.common.Utils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
import reactor.core.publisher.Mono;

import java.util.function.Consumer;

/**
 * 创建flux并发送
 *
 * @author Administrator
 */
public class Lec08FluxCreate {
    public static void main(String[] args) {
//        Flux.create(fluxSink -> {
//            String name;
//            do {
//                name = Utils.faker().country().name();
//                fluxSink.next(name);
//
//            } while (!name.equals("Canada"));
//            fluxSink.complete();
//        }).subscribe(Utils.subscriber());

        var g = new NameGenerator();
        Flux<String> stringFlux = Flux.create(g);
        stringFlux.subscribe(Utils.subscriber());
        for (int i = 0; i < 10; i++) {
            g.generator();
        }

    }

    static class NameGenerator implements Consumer<FluxSink<String>> {
        private FluxSink<String> sink;

        @Override
        public void accept(FluxSink<String> stringFluxSink) {
            this.sink = stringFluxSink;
        }

        public void generator() {
            this.sink.next(Utils.faker().name().firstName());
        }
    }
}
